<?php

namespace App\Admin\Controllers;

use App\Models\Supplier;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SupplierController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '供应商';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Supplier());

        $grid->column('company', __('Company'));
        $grid->column('number', __('Number'));
        $grid->column('name', __('Name'));
        $grid->column('phone', __('Phone'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('status', __('Status'))->switch([
            'on' => ['value' => 1, 'text' => '正常'],
            'off' => ['value' => 2, 'text' => '禁用']
        ]);
        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('updated_at', __('Updated at'))->sortable();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();

            $filter->like('company', '供应商');
            $filter->like('name', '联系人');
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {

            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Supplier::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('company', __('Company'));
        $show->field('number', __('Number'));
        $show->field('province', __('Province'));
        $show->field('city', __('City'));
        $show->field('county', __('County'));
        $show->field('address', __('Address'));
        $show->field('lat', __('Lat'));
        $show->field('lng', __('Lng'));
        $show->field('name', __('Name'));
        $show->field('phone', __('Phone'));
        $show->field('mobile', __('Mobile'));
        $show->field('description', __('Description'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Supplier());

        $form->text('company', __('Company'));
        $form->text('number', __('供应商编号'))
            ->append('<a type="button" id="get_number">生成编码</a>')
            ->setScript("
 $('#get_number').on('click', function() {
    $.ajax({
        type:'post',
        url: '".Route('no')."',
        data:{
            _token:LA.token,
        },
        success: function (res) {
            $('#number').val(res);
            toastr.success('生成成功');
        }
    });
});")->readonly();
        $form->distpicker(['province', 'city', 'county'], '地址选择');
        $form->text('address', __('Address'));
        $form->text('name', __('联系人'));
        $form->mobile('phone', __('Phone'));
        $form->mobile('mobile', __('Mobile'));
        $form->text('description', __('Description'));
        $form->switch('status', __('Status'))
            ->states([
                'on' => ['value' => 1, 'text' => '正常'],
                'off' => ['value' => 2, 'text' => '禁用']
            ])
            ->default(1);

        return $form;
    }

    /**
     * 获取随机16位供应商编号
     */
    public function get_supplierno()
    {
        $a = range(0,9);
        for($i=0;$i<16;$i++){
            $b[] = array_rand($a);
        }
        return join("",$b);
    }
}
